import java.util.LinkedHashMap;
import java.util.Map;

public abstract class BaseScope implements Scope{
    Scope enclosingScope;
    Map<String, Symbol> symbols= new LinkedHashMap<String, Symbol>();

    BaseScope(Scope enclosingScope) {
        this.enclosingScope = enclosingScope;
    }

    @Override
    public Scope getEnclosingScope() {
        return this.enclosingScope;
    }

    @Override
    public void define(Symbol symbol) {
        this.symbols.put(symbol.name, symbol);
        symbol.scope = this;
    }

    @Override
    public Symbol resolve(String name) {
        Symbol symbol = symbols.get(name);
        if (symbol != null) {
            return symbol;
        }
        if (this.enclosingScope != null) {
            return this.enclosingScope.resolve(name);
        }
        return null;
    }
}
